package com.hb.common.service.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hb.common.domain.EduSubject;
import com.hb.common.domain.SubjectData;
import com.hb.common.service.EduSubjectService;
import com.hb.common.mapper.EduSubjectMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.List;

import java.util.HashMap;
import java.util.Map;

/**
 *
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject>
    implements EduSubjectService {

    @Override
    public void saveFromExcel(List<SubjectData> cachedDataList) {
        //优化后的尽量少查数据库
        //把cachedDataList封装成map
        Map<String, List<String>> map = new HashMap<>();
        for (SubjectData subjectData : cachedDataList) {
            List<String> li = map.get(subjectData.getOne());
            if (li == null) li = new ArrayList<>();
            li.add(subjectData.getTwo());
            map.put(subjectData.getOne(), li);
        }
        //为了减少查询语句
        List<EduSubject> subjects = this.list();
        Map<String ,EduSubject> existMap = new HashMap<>();
        for (EduSubject o : subjects) {
            existMap.put(o.getTitle(),o);
        }
        map.forEach((one,twoLi)-> {
            EduSubject existOneSubject=existMap.get(one);
            if(existOneSubject==null){
                //保存一级目录
                existOneSubject=new EduSubject();
                existOneSubject.setTitle( one);
                existOneSubject.setParentId("0");
                this.save(existOneSubject);
            }
            // 备注：如果一级目录已经存在的话只保存二级目录即可
            List<EduSubject> twoSubjects= new ArrayList<>();
            for (String title : twoLi) {
                EduSubject existTwoSubject = existMap.get(title);
                if(existTwoSubject == null){ //判断二级目录去重
                    existTwoSubject = new EduSubject();
                    existTwoSubject.setTitle(title);
                    existTwoSubject.setParentId(existOneSubject.getId());
                    twoSubjects.add(existTwoSubject);
                }
            }
            //批量保存二级目录
            this.saveBatch(twoSubjects);
        });
    }

    @Override
    public List<EduSubject> getAllSubject() {
        List<EduSubject> vos= new ArrayList<>();
        List<EduSubject> list = this.list();//查询所有的分类
        for (EduSubject eduSubject : list) {
            EduSubject subjectVo = new EduSubject();
            BeanUtils.copyProperties(eduSubject,subjectVo);
            vos.add(subjectVo);
        }

        //vos  数据库有几条分类数据就有几个 ，二级和一级全都混合在一起
        //递归的方式查   一直自查 直到查不到数据为止
        List<EduSubject> subjectVos = new ArrayList<>();

        for (EduSubject treeNode : vos) {
            if ("0".equals(treeNode.getParentId ())) {//说明是一级分类
                subjectVos.add(findChildren(treeNode,vos));//就要装填二级分类
            }
        }
        return subjectVos;
    }


    /**
     * 递归查找子节点   自己调用自己
     * @param treeNodes
     * @return
     */

    public static EduSubject findChildren( EduSubject subject,List<EduSubject> treeNodes) {
        subject.setChildren(new ArrayList<>());

        for (EduSubject it : treeNodes) {
            if(subject.getId().equals(it.getParentId())) {
//                subject.getChildren().add(findChildren(it,treeNodes));//把二级分类挂在一级上面(也可以是三级挂在二级上面以此类推)
                subject.getChildren().add(it);//把二级分类挂在一级上面
                findChildren(it,treeNodes);//根据二级找三级，根据三级找四级，以此类推
            }
        }
        System.out.println(subject.getChildren());
        return subject;
    }


}




